﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml.Linq;

using BOL.IO;
using BOL.Apps.GeneNetworkBrowser.ViewModels;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.IO
{
    public sealed class ProjectWriter : IWriter
    {
        #region Private variables

        private FileStream _fs;
        private ProjectTreeViewItemViewModel _project;

        #endregion

        #region Constructors

        public ProjectWriter(FileStream fs, ProjectTreeViewItemViewModel project)
        {
            _fs = fs;
            _project = project;
        }

        public ProjectWriter(string path, ProjectTreeViewItemViewModel project) : this(new FileStream(path, FileMode.Create), project) { } 

        #endregion

        #region IWriter implementation

        public bool Write()
        {
            XDocument xd = new XDocument(
                new XElement("Project",
                    new XAttribute("Version", 0.1),
                        new XElement("Name", _project.Title),
                        new XElement("NetworkGroup",
                            from dataFile in _project.Networks select
                                new XElement("DataFile", dataFile)),
                        new XElement("AnalysisGroup",
                            from networkFile in _project.Analyses select
                                new XElement("NetworkFile", networkFile))));
            xd.Save(_fs);

            return true;
        }

        public void Close()
        {
            _fs.Close();
        }

        #endregion
    }
}
